Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python multi namespace #5375

Open
wants to merge 66 commits into
base: main
Choose a base branch
from
Open

Python multi namespace #5375

wants to merge 66 commits into from

Conversation

msyyc
Copy link
Contributor

@msyyc msyyc commented Dec 16, 2024

For Azure/autorest.python#2795.

Compared to the previous logic that only supported a single namespace, this PR introduces support for multiple namespaces. This primarily requires updates in two key areas:

  1. Serialization approach: We need to account for all namespaces and serialize them individually.
  2. Relative imports: The Python SDK utilizes relative imports internally, and we can no longer make assumptions about the location of models/operations. To determine the correct relative import, we need to know the serialized namespace within the imports function.

Considering these two main changes, here's a detailed summary of the updates in this PR:

  1. Added the emitter flag enable-typespec-namespace, which lets users decide if they want to support native typespec namespace.
  2. Introduced clientNamespace for models/operations/enums/namedUnions/clients: the emitter uses clientNamespace to determine the exact location where the target type should be.
  3. Created get_relative_import_path for CodeModel: all relative import paths should be calculated using this function.
  4. Added a new property client_namespace_types to CodeModel: it counts and stores all namespace types and information about clients/operation_groups/enums/models in each namespace.
  5. Serialization: With the new client_namespace_types property in CodeModel, we can cycle through each namespace and serialize its clients/models/enums/operations using the same logic.
  6. Imports: The old import logic used the relative_path and operation kwargs, which were not set from the top caller but from various functions during the calling process. The signatures were difficult to understand and maintain. I replaces them with the new serialize_namespace and serialize_namespace_type kwargs. Now, any function that needs to calculate relative imports can use these two parameters, which are set from the top caller, making them easier to understand and maintain.

@microsoft-github-policy-service microsoft-github-policy-service bot added the emitter:client:python Issue for the Python client emitter: @typespec/http-client-python label Dec 16, 2024
@azure-sdk
Copy link
Collaborator

No changes needing a change description found.

@azure-sdk
Copy link
Collaborator

You can try these changes here

🛝 Playground 🌐 Website 📚 Next docs

@msyyc msyyc requested a review from tadelesh December 24, 2024 02:00
@@ -43,6 +44,7 @@ const EmitterOptionsSchema: JSONSchemaType<PythonEmitterOptions> = {
debug: { type: "boolean", nullable: true },
flavor: { type: "string", nullable: true },
"examples-dir": { type: "string", nullable: true, format: "absolute-path" },
"enable-typespec-namespace": { type: "boolean", nullable: true },
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: can we add some comment for the new config? i believe we should do such clean up with the pr of consolidating emitter's config name.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added comment for the new flag. And I talked to JS/.NET devs, then they are fine for current name.

packages/http-client-python/emitter/src/utils.ts Outdated Show resolved Hide resolved
Comment on lines +124 to +126
"client/namespace": {
"enable-typespec-namespace": "true",
},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for unbranded. shall we always enable it by default? i heard java do that by default and for unbranded, it is reasonable.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here is generated code for e2e demo allenjzhang/typespec-e2e-demo#51 if we enable typespec namespace. We can see there are additional levels of package structure, which is not intuitive. If SDK users want to call some API, they just need to import client then call it:

from todo import TodoAppClient

client = TodoAppClient()
client.users.get(...)

However, if they need models, they have to make it clear where the model is:

from todo.models import CreateFormResponse
from todo.users.models import UserExistsResponse

Compared with before that users can always import any model from todo.models, the usage experience is not better after enable typespec namespace. So I think we shall default --enable-typepsec-namespace as False which I think is better for most developers. And if typespec is really complicated and typepspec author really want the multinamespace in Python SDK, they could add the flag as True in tspconfig.yaml.

Copy link
Member

@tadelesh tadelesh Dec 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think it is because python does not support initialize the users client directly. to me, users should be a separate client and should be put under the todo.users, then the client, operation, model could be all under todo.users. i prefer to honor the typespec namespace all the time because it could reflect all the spec author's idea. any weird structure should come from a poor spec and author should fix the spec directly.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@iscai-msft / @lmazuel for awareness.


def init_file_import(self) -> FileImport:
return FileImport(self.code_model)

@property
def serialize_namespace(self) -> str:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we add a comment for this? sth like serialize_namespace is the namespace of the serialized file. for example, when we serialize the async operation A with Azure.Test.Foo namespace, then the serialize_namespace should be Azure.Test.Foo.aio._operations.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added.

self._operations_folder_name[client_namespace] = name
return self._operations_folder_name[client_namespace]

def get_serialize_namespace(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i prefer to move this function to base_serializer.py because it is not related with the code model, but for the code serialization. the result for this function is depend on how we put the generated code. same for other helper like operations_folder_name.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

get_serialize_namespace and operations_folder_name use client_namespace_types of code_model and code_model can be accessed in almost all types so it is most convenient way to access them in code_model.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i just want to make sure serializing logic is placed in serializer, code model status and calculation is placed in type model. if they are coupled, then i'm ok with that.

@msyyc msyyc requested a review from tadelesh December 25, 2024 07:37
Copy link
Member

@tadelesh tadelesh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the changes lgtm. but i really think we should do a refactor to separate the logic of serializing and type status in some time. current logic of import and other serialization thing has too many coupling things. another thing is we should hold merge till we add complicated test cases in spector.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
emitter:client:python Issue for the Python client emitter: @typespec/http-client-python
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants